//
//  InLandCityDetailViewController.m
//  旅游咨询
//
//  Created by 高建 on 15/4/13.
//  Copyright (c) 2015年 xubin. All rights reserved.
//

#import "InLandCityDetailViewController.h"

@interface InLandCityDetailViewController ()

@end

@implementation InLandCityDetailViewController


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationController.navigationBar.translucent = NO;
    
//    [self.navigationController setNavigationBarHidden:YES];

    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"<< 返回" style:(UIBarButtonItemStylePlain) target:self action:@selector(popViewRootViewController)];
    
    self.myScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, self.view.frame.size.height / 16 * 6, self.view.frame.size.width, self.view.frame.size.height / 2 + 10)];
    self.myScrollView.backgroundColor = [UIColor purpleColor];
    [self.myScrollView release];
    [self.view addSubview:self.myScrollView];

    self.myScrollView.contentSize = CGSizeMake(self.myScrollView.frame.size.width * 3, 0);
    self.myScrollView.pagingEnabled = YES;
    self.myScrollView.bounces = NO;
    self.myScrollView.delegate = self;
    
    self.myCityTableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.myScrollView.frame.size.height)];
    //取消了横线
    self.myCityTableView.separatorStyle = NO;
    [self.myScrollView addSubview:self.myCityTableView];
    self.myCityTableView.delegate = self;
    self.myCityTableView.dataSource = self;
    [self.myCityTableView release];
    
    self.houseTableView = [[UITableView alloc] initWithFrame:CGRectMake(self.view.frame.size.width, 0, self.view.frame.size.width, self.myScrollView.frame.size.height)];
    self.houseTableView.separatorStyle = NO;
    self.houseTableView.delegate = self;
    self.houseTableView.dataSource = self;
    [self.myScrollView addSubview:self.houseTableView];
    [self.houseTableView release];
    
    self.restaurantTableView = [[UITableView alloc] initWithFrame:CGRectMake(self.view.frame.size.width * 2, 0, self.view.frame.size.width, self.myScrollView.frame.size.height)];
    self.restaurantTableView.separatorStyle = NO;
    self.restaurantTableView.delegate = self;
    self.restaurantTableView.dataSource = self;
    [self.myScrollView addSubview:self.restaurantTableView];
    [self.restaurantTableView release];
    
    //建立一个view
    UIView *headView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width,self.view.frame.size.height / 16 * 6 )];
//    headView.backgroundColor = [UIColor blackColor];
    [self.view addSubview:headView];
    [headView release];
//    self.myCityTableView.tableHeaderView = headView;
    
    //将myimagview添加到view上
    UIImageView *myImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height / 16 * 5)];
    myImageView.backgroundColor = [UIColor greenColor];
    [myImageView sd_setImageWithURL:[NSURL URLWithString:self.MyInLand.cover_route_map_cover]];
    [headView addSubview:myImageView];
    
//    UIButton *aButton = [UIButton buttonWithType:UIButtonTypeCustom];
//    [aButton setBackgroundImage:[UIImage imageNamed:@"iconfont-fanhui-2.png"] forState:UIControlStateNormal];
//    [aButton addTarget:self action:@selector(popAction:) forControlEvents:UIControlEventTouchUpInside];
//    aButton.frame = CGRectMake(15, 25, 30, 30);
//    [myImageView addSubview:aButton];
    
    self.cityLabel = [[UILabel alloc] initWithFrame:CGRectMake(self.view.frame.size.width / 23, headView.frame.size.height / 8 * 4, self.view.frame.size.width / 6 + 20, self.view.frame.size.height / 19)];
    self.cityLabel.font = [UIFont systemFontOfSize:25.0];
    self.cityLabel.textColor = [UIColor whiteColor];
    self.cityLabel.text = self.MyInLand.name_zh;
    
    [headView addSubview:_cityLabel];
    [_cityLabel release];
    

    //添加一个图片的轻拍手势
    //打开用户交互
    myImageView.userInteractionEnabled = YES;
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapAction:)];
    [myImageView addGestureRecognizer:tap];
    [tap release];
    
//    //创建不能错过按钮
//    UIButton *MissButton = [UIButton buttonWithType:UIButtonTypeCustom];
//    MissButton.backgroundColor = [UIColor blueColor];
//    MissButton.frame = CGRectMake(0, headView.frame.size.height / 8 * 5, self.view.frame.size.width / 2, headView.frame.size.height / 8);
////    [MissButton setTitle:@"Miss" forState:UIControlStateNormal];
//    [MissButton addTarget:self action:@selector(missButtonAction:) forControlEvents:UIControlEventTouchUpInside];
////    [MissButton setBackgroundImage:[UIImage imageNamed:@"renzheng.png"] forState:UIControlStateNormal];
//    [headView addSubview:MissButton];
//    
//    //创建实用须知按钮
//    UIButton *NeedButton = [UIButton buttonWithType:UIButtonTypeCustom];
//    NeedButton.backgroundColor = [UIColor blueColor];
////    [NeedButton setTitle:@"Need" forState:UIControlStateNormal];
//    NeedButton.frame = CGRectMake(self.view.frame.size.width / 2, headView.frame.size.height / 8 * 5, self.view.frame.size.width / 2, headView.frame.size.height / 8);
//    [NeedButton addTarget:self action:@selector(needButtonAction:) forControlEvents:UIControlEventTouchUpInside];
////    [NeedButton setBackgroundImage:[UIImage imageNamed:@"paihang.png"] forState:UIControlStateNormal];
//    [headView addSubview:NeedButton];
    
    //建立热门地点label
//    UILabel *myLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, headView.frame.size.height / 4 * 3, self.view.frame.size.width, headView.frame.size.height / 8)];
//    myLabel.backgroundColor = [UIColor yellowColor];
//    myLabel.text = @"热门地点";
//    myLabel.textAlignment = NSTextAlignmentCenter;
//    [headView addSubview:myLabel];
    
    //segmentC创建
    NSMutableArray *array = [NSMutableArray arrayWithObjects:@"景点",@"住宿",@"餐厅", nil];
    self.segmentC = [[UISegmentedControl alloc]initWithItems:array];
    self.segmentC.frame = CGRectMake(0, self.view.frame.size.height / 16 * 5, self.view.frame.size.width, self.view.frame.size.height / 16);
    [headView addSubview:self.segmentC];
    [self.segmentC addTarget:self action:@selector(segmentChangeAction:) forControlEvents:UIControlEventValueChanged];
    self.segmentC.selectedSegmentIndex = 0;
    
    
    //AFN数据请求
    [MyAFNData getNetData:self.url responseGETData:^(id data) {
        self.CityPlaceArray = [NSMutableArray array];
        NSMutableArray *array = [data objectForKey:@"items"];
        for (NSMutableDictionary *CityPlaceDic in array) {
            InLandCityDetailModel *myDetailModel = [[InLandCityDetailModel alloc] init];
            [myDetailModel setValuesForKeysWithDictionary:CityPlaceDic];
            myDetailModel.photoId = [CityPlaceDic objectForKey:@"id"];
            myDetailModel.descriptions = [CityPlaceDic objectForKey:@"description"];
            [self.CityPlaceArray addObject:myDetailModel];
            [myDetailModel release];
        }
        [self.myCityTableView reloadData];

    }];
    
    [MyAFNData getNetData:self.myhotelUrl responseGETData:^(id data) {
        self.HouseViewArray = [NSMutableArray array];
        NSMutableArray *array = [data objectForKey:@"items"];
        for (NSMutableDictionary *HouseDic in array) {
            HouseTableView *myHouseModel = [[HouseTableView alloc] init];
            [myHouseModel setValuesForKeysWithDictionary:HouseDic];
            myHouseModel.descriptions = [HouseDic objectForKey:@"description"];
            myHouseModel.photoId = [HouseDic objectForKey:@"id"];
            [self.HouseViewArray addObject:myHouseModel];
//            NSLog(@"woshi house id +++++%@", myHouseModel.photoId);
            [myHouseModel release];
        }
                [self.houseTableView reloadData];
    }];
    
    [MyAFNData getNetData:self.myRestaurantUrl responseGETData:^(id data) {
        self.RestaurantViewArray = [NSMutableArray array];
        NSMutableArray *array = [data objectForKey:@"items"];
        for (NSMutableDictionary *RestaurantDic in array) {
            RestaurantTableView *myRetaurantModel = [[RestaurantTableView alloc] init];
            [myRetaurantModel setValuesForKeysWithDictionary:RestaurantDic];
            myRetaurantModel.descriptions = [RestaurantDic objectForKey:@"description"];
            myRetaurantModel.photoId = [RestaurantDic objectForKey:@"id"];
            [self.RestaurantViewArray addObject:myRetaurantModel];
            [myRetaurantModel release];
        }
                [self.restaurantTableView reloadData];
    }];
}
- (void)popViewRootViewController
{
    [self.navigationController popViewControllerAnimated:YES];
    
}
- (void)segmentChangeAction:(UISegmentedControl *)segmentChange
{
    if (segmentChange.selectedSegmentIndex == 0) {
        [UIView animateWithDuration:0.3 animations:^{
            self.myScrollView.contentOffset = CGPointMake(0, 0);
            
        }];
        
    } else if (segmentChange.selectedSegmentIndex == 1) {
        [UIView animateWithDuration:0.3 animations:^{
            self.myScrollView.contentOffset = CGPointMake(self.view.frame.size.width, 0);
            
        }];
        
    }else if( segmentChange.selectedSegmentIndex == 2)
    {
        [UIView animateWithDuration:0.3 animations:^{
            self.myScrollView.contentOffset = CGPointMake(self.view.frame.size.width * 2, 0);
            
        }];
    }
}

-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    int index = self.myScrollView.contentOffset.x / self.myScrollView.frame.size.width;
    
    self.segmentC.selectedSegmentIndex = index;
    
}


//轻拍手势方法
- (void)tapAction:(UITapGestureRecognizer *)tapAction
{
    FirstPhotoViewController *firstPhoto = [[FirstPhotoViewController alloc]init];
    [self.navigationController pushViewController:firstPhoto animated:YES];
    firstPhoto.MyFirstPhoto = self.FirstPhotoUrl;
}

//-(void)missButtonAction:(UIButton *)missButton
//{
//    NoMissViewController *myNoMiss = [[NoMissViewController alloc]init];
//    myNoMiss.NoMissUrl = self.MissButtonUrl;
//    myNoMiss.MissVC = self;
//    
//    [self.navigationController pushViewController:myNoMiss animated:YES];
//
//}
//- (void)needButtonAction:(UIButton *)needButton
//{
//    NeedKnowViewController *myNeedKnow = [[NeedKnowViewController alloc]init];
//    myNeedKnow.NeedUrl = self.NeedButtonUrl;
//    myNeedKnow.NeedVC = self;
//    
//    [self.navigationController pushViewController:myNeedKnow animated:YES];
//    
//}


-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return HeighZ * 0.2;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (tableView == self.myCityTableView) {
        return self.CityPlaceArray.count;
    }else if(tableView == self.houseTableView){
        return self.HouseViewArray.count;
    }else{
        return self.RestaurantViewArray.count;
    }
    
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (tableView == self.myCityTableView) {
        static NSString *cellIdentifier = @"cell";
        LandCityTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
        if (cell == nil) {
            cell = [[LandCityTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
        }
        InLandCityDetailModel *myCityDetail = [self.CityPlaceArray objectAtIndex:indexPath.row];
        cell.myDetailModel = myCityDetail;
        cell.selectionStyle = 0;
//        cell.backgroundColor = [UIColor redColor];
        return cell;
        
    } else if(tableView == self.houseTableView) {
        static NSString *cellIdentifier = @"cellHouse";
        LandCityTableViewCell *cellHouse = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
        if (cellHouse == nil) {
            cellHouse = [[LandCityTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
        }
        HouseTableView *myHouse = [self.HouseViewArray objectAtIndex:indexPath.row];
        cellHouse.myHouseModel = myHouse;
        cellHouse.selectionStyle = 0;
        
        return cellHouse;
        
    } else {
        static NSString *cellIdentifier = @"cellRest";
        LandCityTableViewCell *cellRest = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
        if (cellRest == nil) {
            cellRest = [[LandCityTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
        }
        RestaurantTableView *myRestaurant = [self.RestaurantViewArray objectAtIndex:indexPath.row];
        cellRest.myRestaurantModel = myRestaurant;
        cellRest.selectionStyle = 0;
        
        return cellRest;
        
    }
    
//    NSLog(@"_+_+_+_+_+_+_+_+_+_+_+_+_+_+_+_+_+%@",cell.myDetailModel);
    
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    HotSiteDetailViewController *hotSite = [[HotSiteDetailViewController alloc]init];
    
    if (tableView == self.myCityTableView) {
        //model对象
        hotSite.myHotSiteDetail = [self.CityPlaceArray objectAtIndex:indexPath.row];
        
    }else if(tableView == self.houseTableView){
        hotSite.myHotSiteDetail = [self.HouseViewArray objectAtIndex:indexPath.row];
        
    }else{
        hotSite.myHotSiteDetail = [self.RestaurantViewArray objectAtIndex:indexPath.row];
    }
    //数组
//    hotSite.mainHotArray = self.CityPlaceArray;
    
//    NSLog(@"+++++++++++++++%@",hotSite.mainHotArray);
    [self.navigationController pushViewController:hotSite animated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
